<?php
namespace Kuoqi\Api\Weixin;

use Kuoqi\Curl;

/**
 * Class Fans
 * @package Kuoqi\Api\Weixin\Applet
 * User kuoqi
 * Datetime 2022/3/2 16:21
 * Remark 偏向业务模块
 */
class Tools{


    /**
     * @param string $url
     * @param string $method
     * @param array $params
     * @param string $rsptype
     * @param int $timeout
     * @return mixed
     * User kuoqi
     * Datetime 2022/3/2 16:48
     * Remark 发送数据信息
     */
    static public function sendTo($url = '', $method = '', $params = array(), $rsptype = 'json',$timeout = 0){
        return \Kuoqi\Curl::sendTo($url, $method, $params, $rsptype,$timeout);
    }

    /**
     * @param $rs
     * @return array
     * User kuoqi
     * Datetime 2022/3/2 16:49
     * Remark 响应格式配置
     */
    static public function response($rs){
        if(isset($rs['errcode'])&&$rs['errcode']){
            //错误处理
            return array('errcode'=>$rs['errcode'],'errmsg'=>$rs['errmsg']);
        }else if(isset($rs['errcode'])){
            new \Kuoqi\Error('等待开发');
        }else{
            return array('errcode'=>0,'errmsg'=>'ok','data'=>$rs);
        }
    }

    /**
     * xml2array xml转数组
     * @param $xml 输入的xml文件
     * @return mixed 响应的数组
     */
    static public function xmlToArray($xml){
        $xml = preg_replace('/\<\!\[CDATA\[(.+)\]\]\>/', htmlentities('$1'), $xml);
        $data =  @simplexml_load_string($xml);
        return json_decode(json_encode($data),true);
    }

    //需要外部实现配置
    static function setCache($n,$v,$t = 3600){
        return \Kuoqi\Cache::setCache($n,$v,$t,'',true);
    }

    //需要实现外部方法
    static function getCache($n){
        //tido--获取缓存配置模块
        return \Kuoqi\Cache::getCache($n,'',true);
    }

    /**
     * 验签md5
     * createby kuoqi at 2022/10/23 17:57
     * @param array $params
     * @param string $key
     * @return string
     */
    static function signMd5($params = array(),$key = ''){
        ksort($params);
        $sign='';
        foreach($params as $k=>$v){
            $sign.=$k.'='.$v.'&';
        }
        $sign .='key='.$key;
        return strtoupper(md5($sign));//md5+大写
    }

}